package com.yangt.controller;

import com.alibaba.fastjson.JSON;
import com.yangt.beacon.common.concurrent.AsdThreadPool;
import com.yangt.beacon.common.utils.YtTraceContext;
import com.yangt.log.YtLoggerFactory;
import com.yangt.log.model.YtLogger;
import com.yangt.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author hongbin.yang
 * @date 2016-05-23 20:32
 */
@Controller
public class DemoController {

    /**
     * 这里是调用的远程服务bean
     */
    @Autowired
    private MainService mainService;

    @Autowired
    private AsdThreadPool asdThreadPool;

    @RequestMapping("/log.do")
    @ResponseBody
    public String doCheckss(String name) {
        try {
            //YtTraceContext.createRootSpan();
            YtLogger log = YtLoggerFactory.getLogger(YtLogger.class, "ytTraceLogger");
            log.bizType("333").info("3333");
            return "ok";
        } finally {
            //System.out.print("--------->" + YtTraceContext.get());
            //YtTraceContext.remove();
        }
    }

    @RequestMapping("/simpleFacade.do")
    @ResponseBody
    public String doCheck(String name) {
        try {
            //YtTraceContext.createRootSpan();
            return mainService.sayHello(name);
        } finally {
            //System.out.print("--------->" + YtTraceContext.get());
            //YtTraceContext.remove();
        }
    }

    @RequestMapping("/link.do")
    @ResponseBody
    public String doCheck2(String name) {
        try {
            YtTraceContext.createRootSpan();
            return JSON.toJSONString(mainService.getUserInfo(name));
        } finally {
            //System.out.print("--------->" + YtTraceContext.get());
            YtTraceContext.remove();
        }
    }

    @RequestMapping("/runnable.do")
    @ResponseBody
    public String doCheck3(String name) {
        try {
           // YtTraceContext.createRootSpan();
            Runnable task = new Runnable() {

                @Override
                public void run() {
                    mainService.sayHello(name);
                }
            };
            new Thread(task).start();
            return "ok";
        } finally {
            System.out.print("--------->" + YtTraceContext.get());
           // YtTraceContext.remove();
        }
    }

    @RequestMapping("/callable.do")
    @ResponseBody
    public String doCheck4(String name) throws ExecutionException, InterruptedException {
        try {
            YtTraceContext.createRootSpan();
            Callable<String> task = new Callable<String>() {

                @Override
                public String call() throws Exception {
                    return mainService.sayHello(name);
                }
            };
            ExecutorService pool = Executors.newFixedThreadPool(1);
            Future<String> t = pool.submit(task);//asdThreadPool.submit(task);
            return " callable ok" + t.get();
        } finally {
            System.out.print("--------->" + YtTraceContext.get());
             YtTraceContext.remove();
        }
    }

}
